/*
 * @lc app=leetcode.cn id=541 lang=java
 *
 * [541] 反转字符串 II
 */

// @lc code=start
class Solution {
    public String reverseStr(String s, int k) {
        char[] ch=s.toCharArray();
        for(int i=0;i<s.length();i+=2*k){
            if(i+k<=ch.length){
                reverse(ch, i, i+k-1);
                continue;
            }
            reverse(ch, i, ch.length-1);

        }
        return new String(ch);
    }
    public void reverse(char[] ch,int i,int j ){
        while(i<j){
            ch[i]^=ch[j];
            ch[j]^=ch[i];
            ch[i]^=ch[j];
            i++;
            j--;
        }
    }
}
// @lc code=end

